package com.mes.biz.service.impl;

import java.util.List;

import com.mes.biz.domain.BizProductionProcess;
import com.mes.biz.domain.vo.ProcessCompletionVo;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.biz.mapper.BizProductProcessPlanMapper;
import com.mes.biz.domain.BizProductProcessPlan;
import com.mes.biz.service.BizProductProcessPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 产品工序计划Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-05-03
 */
@Service
@Transactional(readOnly = true)
public class BizProductProcessPlanServiceImpl extends BaseServiceImpl<BizProductProcessPlanMapper, BizProductProcessPlan> implements BizProductProcessPlanService {

    private static final Logger log = LoggerFactory.getLogger(BizProductProcessPlanServiceImpl.class);

    @Resource
    private BizProductProcessPlanMapper bizProductProcessPlanMapper;

    /**
     * 获取单条数据
     * @param bizProductProcessPlan 产品工序计划
     * @return 产品工序计划
     */
    @Override
    public BizProductProcessPlan get(BizProductProcessPlan bizProductProcessPlan) {
        BizProductProcessPlan dto = super.get(bizProductProcessPlan);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 产品工序计划id
     * @return 产品工序计划
     */
    @Override
    public BizProductProcessPlan get(String id) {
        BizProductProcessPlan dto = super.get(id);
        return dto;
    }

    /**
     * 查询产品工序计划列表
     * @param bizProductProcessPlan 产品工序计划
     * @return 产品工序计划
     */
    @Override
    public List<BizProductProcessPlan> findList(BizProductProcessPlan bizProductProcessPlan) {
		List<BizProductProcessPlan> bizProductProcessPlanList = super.findList(bizProductProcessPlan);
        return bizProductProcessPlanList;
    }

    /**
     * 分页查询产品工序计划列表
     * @param bizProductProcessPlan 产品工序计划
     * @return 产品工序计划
     */
    @Override
    public PageInfo<BizProductProcessPlan> findPage(BizProductProcessPlan bizProductProcessPlan) {
		PageInfo<BizProductProcessPlan> page = super.findPage(bizProductProcessPlan);
        return page;
    }

    /**
     * 保存产品工序计划
     * @param bizProductProcessPlan
     * @return 结果
     */
    @Override
    public boolean save(BizProductProcessPlan bizProductProcessPlan) {
        return super.save(bizProductProcessPlan);
    }

    /**
     * 删除产品工序计划信息
     * @param bizProductProcessPlan
     * @return 结果
     */
    @Override
    public boolean remove(BizProductProcessPlan bizProductProcessPlan) {
        return super.remove(bizProductProcessPlan);
    }

    /**
     * 批量删除产品工序计划
     * @param ids 需要删除的产品工序计划ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizProductProcessPlanByIds(String[] ids) {
        return mapper.deleteBizProductProcessPlanByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public boolean saveBatch(List<BizProductProcessPlan> bizProductProcessPlanList) {
        //查询工序计划
        String processPlanId = bizProductProcessPlanList.get(0).getProcessPlanId();
        //删除工序计划明细
        mapper.deleteByProcessPlanId(processPlanId,BaseEntity.DEL_FLAG_DELETE);
        mapper.saveBatch(bizProductProcessPlanList);
        return true;
    }

    @Override
    @Transactional
    public boolean add(BizProductProcessPlan bizProductProcessPlan) {
        //查询工序计划
        String productId = bizProductProcessPlan.getProductId();
        //删除工序计划明细
        mapper.deleteByProductId(productId,BaseEntity.DEL_FLAG_DELETE);
        if (StringUtils.isNotEmpty(bizProductProcessPlan.getProcessPlanId())){
            bizProductProcessPlan.setTenantId(SecurityUtils.getTenantId());
            super.save(bizProductProcessPlan);
        }
        return true;
    }

    @Override
    public List<BizProductProcessPlan> getBizProductProcessPlanByProductId(String productId) {
        return bizProductProcessPlanMapper.getBizProductProcessPlanByProductId(productId);
    }

    @Override
    public List<BizProductionProcess> getCompletionByProductId(String productId) {
        return bizProductProcessPlanMapper.getCompletionByProductId(productId);
    }

}
